// Author: dolphinigle
// 28 March 2012

var last_liked_id = -1;
var cnt = 0;

function PutLikes(id, user) {
  
  // Ajax
  xmlhttp=new XMLHttpRequest();
  xmlhttp.open("GET",
               "like_query.php?t=" + Math.random() + "&type=likes&id=" + id,
               false);
  xmlhttp.send();
  var likes = xmlhttp.responseText;
  
  var addum = '';
  
  if (user != '') {
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET",
                 "i_like_query.php?t=" + Math.random() +
                 "&type=likes&id=" + id +
                 "&username=" + user,
                 false);
    xmlhttp.send();
    addum = xmlhttp.responseText;
  }
  
  var text = "+" + likes;
  if (addum == "YES") {
    text = "<b>" + text + "</b>";
  }
  
  document.getElementById("like_count" + id).innerHTML = text;
  
  if (last_liked_id != id) {
    cnt = 0;
  }
  
  last_liked_id = id;
  cnt += 1;
  
  if (cnt == 10) {
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET",
                 "achievement_query.php?t=" + Math.random() +
                 "&type=add&id=7" +
                 "&username=" + user,
                 false);
    xmlhttp.send();
    $res = xmlhttp.responseText;
    if ($res != '') {
      confirm("Achievement Unlocked: " + $res);
    }
  }
}

function PutDislikes(id, user) {

  // Ajax
  xmlhttp=new XMLHttpRequest();
  xmlhttp.open("GET",
               "like_query.php?t=" + Math.random() + "&type=dislike&id=" + id,
               false);
  xmlhttp.send();
  var dislikes = xmlhttp.responseText;

  var addum = '';
  if (user != '') {
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET",
                 "i_like_query.php?t=" + Math.random() +
                 "&type=dislike&id=" + id +
                 "&username=" + user,
                 false);
    xmlhttp.send();
    addum = xmlhttp.responseText;
  }
  
  var text = "-" + dislikes;
  if (addum == "YES") {
    text = "<b>" + text + "</b>";
  }
  
  document.getElementById("dislike_count" + id).innerHTML = text;
}

function LikeClicked(id, username) {
  // Ajax
  xmlhttp=new XMLHttpRequest();
  xmlhttp.open("GET",
               "like_modify.php?t=" + Math.random() + "&type=likes" +
               "&id=" + id +
               "&username=" + username,
               false);
  xmlhttp.send();
  
  PutLikes(id, username);
}

function DislikeClicked(id, username) {
  // Ajax
  xmlhttp=new XMLHttpRequest();
  xmlhttp.open("GET",
               "like_modify.php?t=" + Math.random() + "&type=dislike" +
               "&id=" + id +
               "&username=" + username,
               false);
  xmlhttp.send();
  $result = xmlhttp.responseText;
  
  PutDislikes(id, username);
}
